package cn.jinjimi.app.main.adapter;

import android.animation.ValueAnimator;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import cn.jinjimi.app.R;
import cn.jinjimi.app.entity.ZdbProduct;
import cn.jinjimi.app.main.adapter.extend.MyBaseAdapter;
import cn.jinjimi.app.main.widget.FrescoImageView.FrescoImageView;

/**
 * Created by lforxeverc on 16/8/15.
 */
public abstract class ZdbCommonListAdapter extends MyBaseAdapter<ZdbProduct> {
    private static final String TAG = "ZdbCommonListAdapter";
    private Context mContext;
    public ZdbProduct deletedProduct;
    private boolean blockClick = false;
    private int delPosition = -1;
    private boolean animating = false;

    public ZdbCommonListAdapter(Context ctx) {
        mContext = ctx;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_zdb_common, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final ZdbProduct product = getItem(position);
        holder.tvTitle.setText(product.name);
        holder.ivLogo.loadView(product.iconUrl);
        holder.tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (shouldBlock()) {
                    return;
                }
                blockClick = true;
                delPosition = position;
                holder.showDelete();

            }
        });
        holder.tvShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (shouldBlock()) {
                    return;
                }
                onShareProduct(getItem(position));
            }
        });

        holder.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                blockClick = false;
                holder.dismissDelete();
                deletedProduct = getItem(position);
                onDeleteProduct(deletedProduct);
            }
        });

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (shouldBlock()) {
                    return;
                }
                onClickProduct(getItem(position), position);
            }
        });
        if (position == delPosition) {
            holder.dismissDelete();
        }

        return convertView;
    }

    public void deleteTargetProduct() {
        remove(deletedProduct);
    }

    private class ViewHolder {
        public TextView tvTitle, tvShare, tvDelete;
        public FrescoImageView ivLogo;
        public Button btnDelete;

        public ViewHolder(View v) {
            tvTitle = (TextView) v.findViewById(R.id.tv_title);
            tvShare = (TextView) v.findViewById(R.id.tv_share);
            tvDelete = (TextView) v.findViewById(R.id.tv_delete);
            ivLogo = (FrescoImageView) v.findViewById(R.id.iv_logo);
            btnDelete = (Button) v.findViewById(R.id.btn_del);

        }

        public void showDelete() {
            if (animating) {
                return;
            }
            animating = true;
            final ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) btnDelete.getLayoutParams();
            final int endValue = 0;
            final ValueAnimator an = ValueAnimator.ofInt(lp.rightMargin, endValue);
            an.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    int value = (int) valueAnimator.getAnimatedValue();
                    lp.rightMargin = value;
                    btnDelete.setLayoutParams(lp);
                    if (!blockClick) {
                        an.cancel();
                        animating = false;
                        btnDelete.postInvalidate();
                    }
                    if (value == endValue) {
                        animating = false;
                        btnDelete.postInvalidate();
                    }
                }

            });
            an.setDuration(300);
            an.start();

        }

        public void dismissDelete() {
            if (animating) {
                return;
            }
            animating = true;
            final ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) btnDelete.getLayoutParams();
            final int endValue = -(btnDelete.getWidth() + 1);
            ValueAnimator an = ValueAnimator.ofInt(lp.rightMargin, endValue);
            an.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    int value = (int) valueAnimator.getAnimatedValue();
                    lp.rightMargin = value;
                    btnDelete.setLayoutParams(lp);
                    if (value == endValue) {
                        animating = false;
                        btnDelete.postInvalidate();
                        delPosition = -1;
                    }
                }

            });
            an.setDuration(300);
            an.start();
        }

    }

    public boolean isBlockClick() {
        return blockClick;
    }

    public void resetView() {
        notifyDataSetChanged();
    }

    public void resetStatus() {
        blockClick = false;
    }

    private boolean shouldBlock() {
        if (blockClick) {
            resetView();
            resetStatus();
            return true;
        }
        return false;
    }

    public abstract void onDeleteProduct(ZdbProduct zdbProduct);

    public abstract void onShareProduct(ZdbProduct zdbProduct);

    public abstract void onClickProduct(ZdbProduct zdbProduct, int position);

}
